﻿package org.lively3d.materials
{
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;

import org.papervision3d.core.geom.renderables.Triangle3D;
import org.papervision3d.core.material.TriangleMaterial;
import org.papervision3d.core.render.data.RenderSessionData;
import org.papervision3d.core.render.draw.ITriangleDrawer;

/**
* The ColorMaterial class creates a solid color material.
*
* Materials collects data about how objects appear when rendered.
*
*/
public class ColorWireframeMaterial extends TriangleMaterial implements ITriangleDrawer
{
	
	
	
	//Color of the wireframe
	public var fillWireColor:Number; 
	//Alpha of the wireframe
	public var fillWireAlpha:Number;
	//Visiblility of the wireframe
	public var	myWireVisible:Boolean=false;
	
	
	
	
	
	/**
	* The ColorMaterial class creates a solid color material.
	*
	* @param	asset				A BitmapData object.
	*/
	public function ColorWireframeMaterial( color:Number=0xFF00FF, alpha:Number = 1,colorWire:Number=0xFFFFFF, alphaWire:Number=1, thickness:Number = 0, wireVisible:Boolean=false, interactive:Boolean = false )
	{
		//Solid Material
		this.fillColor = color;
		this.fillAlpha = alpha;
		
		//Wire Material
		this.fillWireColor = colorWire;
		this.fillWireAlpha = alphaWire*100;
		this.lineThickness=thickness;
		this.myWireVisible=wireVisible;
		
		this.interactive = interactive;
	}
	
	/**
	 *  drawTriangle
	 */
	override public function drawTriangle(face3D:Triangle3D, graphics:Graphics, renderSessionData:RenderSessionData, altBitmap:BitmapData = null, altUV:Matrix = null):void
	{
		
		
		var x0:Number = face3D.v0.vertex3DInstance.x;
		var y0:Number = face3D.v0.vertex3DInstance.y;
		var x1:Number = face3D.v1.vertex3DInstance.x;
		var y1:Number = face3D.v1.vertex3DInstance.y;
		var x2:Number = face3D.v2.vertex3DInstance.x;
		var y2:Number = face3D.v2.vertex3DInstance.y;
		
		
	//Color Piece
	
		graphics.beginFill( fillColor, fillAlpha );
		graphics.moveTo( x0, y0 );
		graphics.lineTo( x1, y1 );
		graphics.lineTo( x2, y2 );
		graphics.lineTo( x0, y0 );
		graphics.endFill();
			
	//Wireframe Piece
	
		if( myWireVisible)
			{ 
				graphics.lineStyle( lineThickness, fillWireColor, fillWireAlpha );
				graphics.moveTo( x0, y0 );
				graphics.lineTo( face3D.v1.vertex3DInstance.x, face3D.v1.vertex3DInstance.y );
				graphics.lineTo( face3D.v2.vertex3DInstance.x, face3D.v2.vertex3DInstance.y );
				graphics.lineTo( x0, y0 );
				graphics.lineStyle();

				
			}
		
		renderSessionData.renderStatistics.triangles++;
	
	}
	
	/**
	* Returns a string value representing the material properties in the specified ColorMaterial object.
	*
	* @return	A string.
	*/
	public override function toString(): String
	{
		return 'ColorMaterial - color:' + this.fillColor + 'WireframeMaterial - color:' + this.lineColor +' alpha:' + this.fillAlpha;
	}
}
}